Mathematica Connectivity

Todd Gayley

Wolfram Research

Connectivity Features in Mathematica

Import/Export

Run, RunThrough

Internet   (HTTP, FTP, SendMail)

Databases   (DatabaseLink)

Wolfram|Alpha   (Ctrl-=, WolframAlpha[] function)

Connectivity Features in Mathematica

Import/Export

Run, RunThrough

Internet   (HTTP, FTP, SendMail)

Databases   (DatabaseLink)

Wolfram|Alpha   (Ctrl-=, WolframAlpha[] function)

MathLink

J/Link

.NET/Link

CUDALink/OpenCLLink

LibraryLink

PythonLink

RLink   (new in version 9)

New HTTP Functionality in Version 9

Internet access code has been completely rewritten in version 9.

Import still works as always for HTTP and FTP URLs, but for more control or to download a file directly to disk, use the new URL functions:

        URLFetch, URLFetchAsynchronous   (read contents of a URL into Mathematica)

        URLSave, URLSaveAsynchronous   (download contents of a URL to a file)

URLSave["ftp://math.nist.gov/pub/MatrixMarket2/NEP/dwave/dwa512.mtx.gz", "c:/foo.gz"]

URLSaveAsynchronous["http://exampledata.wolfram.com/5mb.dat", "c:/foo.dat", callback]

callback[asyncObject_, "statuscode", data_] := Print[data]

New HTTP Functionality in Version 9

POST requests for either Import or the URL functions:

Import["url", "RequestMethod"->"POST"]

Other features: Fine-grained control of cookies and other header data, OAuth authentication support, multipart/form-data uploads, etc.

MathLink

MathLink is a mechanism for exchanging Mathematica expressions between programs.

The Mathematica front end and kernel communicate via MathLink.

MathLink is at the core of a large number of Mathematica-related technologies and capabilities.

A C-language library.

MathLink

“Installable” Programs

MathLink introduced a very powerful new capability into the Mathematica world—the ability to extend the Mathematica environment by plugging in, or “installing,” external C-language functions.

If you have a C function, say int addtwo(int x, int y), you write a simple template file that describes the mapping between a Mathematica function, say AddTwo, and the C function. You then process the template into a C file and compile it all into an executable.

The CCompilerDriver package makes it easy to build MathLink programs.

Needs["CCompilerDriver`"];

examplesDir=FileNameJoin[{$InstallationDirectory,"SystemFiles","Links","MathLink","DeveloperKit",$SystemID,"MathLinkExamples", "addtwo"}];
files=FileNames["*",examplesDir]

exe=CreateExecutable[files,"addtwo"]

Install[exe]

AddTwo[2,3]

MathLink

MathLink can also be used to write programs that launch and call Mathematica for computational services.

J/Link

J/Link is a toolkit that integrates Mathematica and Java. It lets you call Java from Mathematica in a completely transparent way, and it lets you call Mathematica from Java using a high-level API that is easier to use than the C MathLink API.

J/Link

J/Link is a toolkit that integrates Mathematica and Java. It lets you call Java from Mathematica in a completely transparent way, and it lets you call Mathematica from Java using a high-level API that is easier to use than the C MathLink API.

Because J/Link lets you load arbitrary Java classes into Mathematica, create objects, and call methods, it makes all of Java an immediate extension to the Mathematica environment.
There is a staggering amount of software development going on in Java. Having access to all that work directly in Mathematica is a tremendous benefit.

J/Link

Example: formatting numbers.

Needs["JLink`"]
InstallJava[];

fmt = JavaNew["java.text.DecimalFormat", "#.0000"]

The @ symbol is used like the Java . (dot). This means "call the fmt object's format method."

fmt@format[12.34]

.NET/Link

.NET/Link does for Microsoft's .NET environment what J/Link does for Java.

You can load .NET types into Mathematica, create objects, call methods, etc. You can also write programs that call Mathematica using a high-level interface in any .NET language (C#, Visual Basic .NET, C++, Python, and many others).

Needs["NETLink`"]
InstallNET[]

serialPort = NETNew["System.IO.Ports.SerialPort", "COM8", 115200]

NETTypeInfo[serialPort]

serialPort@Open[]

serialPort@Write["data"]

LibraryLink

LibraryLink allows DLLs to be loaded directly into the Mathematica kernel process. There is no inter-process communication, and the need for MathLink is eliminated.

LibraryLink provides a high-speed interface, essentially as if you were able to write new parts of the Mathematica kernel yourself. Because LibraryLink can read and write native Mathematica packed arrays, you can get extremely fast data transfer to and from Mathematica--no copying needed.

LibraryLink

LibraryLink allows DLLs to be loaded directly into the Mathematica kernel process. There is no inter-process communication, and the need for MathLink is eliminated.

LibraryLink provides a high-speed interface, essentially as if you were able to write new parts of the Mathematica kernel yourself. Because LibraryLink can read and write native Mathematica packed arrays, you can get extremely fast data transfer to and from Mathematica--no copying needed.

As is the case with writing an installable MathLink program, you have to write specialized C “glue” code that uses the LibraryLink API to acquire arguments from Mathematica and return results. You cannot simply plug in an arbitrary DLL.

LibraryLink

LibraryLink allows DLLs to be loaded directly into the Mathematica kernel process. There is no inter-process communication, and the need for MathLink is eliminated.

LibraryLink provides a high-speed interface, essentially as if you were able to write new parts of the Mathematica kernel yourself. Because LibraryLink can read and write native Mathematica packed arrays, you can get extremely fast data transfer to and from Mathematica--no copying needed.

As is the case with writing an installable MathLink program, you have to write specialized C “glue” code that uses the LibraryLink API to acquire arguments from Mathematica and return results. You cannot simply plug in an arbitrary DLL.

If your library crashes, the whole kernel will crash with it.

LibraryLink

A simple example of a LibraryLink C function (from the ARDrone control library):

DLLEXPORT mint emergency(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res) {

	int emrgcy = MArgument_getInteger(Args[0]);
	int result = ardrone_tool_set_ui_pad_select(emrgcy);
	MArgument_setInteger(Res, result);
	return LIBRARY_NO_ERROR;
}

Once the above code has been compiled (some other required parts have been omitted) into a library, you load it into Mathematica like this:

libfile = FindLibrary["ARDrone"]

emergencyFunc = LibraryFunctionLoad[libfile,"emergency",{Integer},Integer];

Call it like any other Mathematica function:

emergencyFunc[42]

LibraryLink

You can use the CCompilerDriver` function CreateLibrary to build LibraryLink libraries, which is a great convenience.

Needs["CCompilerDriver`"]

CreateLibrary[
    {"file1.c", "file2.c"},
     "MyLibraryName",
    "TargetDirectory"->"outputDir",
    "Defines"->"DEBUG",
    "CompileOptions"->"-O1"
]

RLink  (new in version 9)

R is an open-source programming language and software environment for statistical computing and graphics.

RLink is a Mathematica application that provides a link to R. It allows the user to communicate data between Mathematica and R, and execute R code from within Mathematica.

<<RLink`

InstallR[]

RSet["vec", {0,1,1,2,3,5,8,13,21,34}];

REvaluate["
{
mx<- mean(vec)
medx<-median(vec)
sdx <- sd(vec)
varx<- var(vec)
results <-c(mx,medx,sdx,varx)
}"]

CUDALink and OpenCLLink

CUDA and OpenCL are programming languages that allow you to run programs on the processor on your graphics card (GPU).

CUDALink and OpenCLLink provide a way to call programs directly from Mathematica. MathLink is not involved.

Deciding Which Technology is Right for Your Project

Question 1: Is there an existing link or feature that already does what I want?

DatabaseLink

HTTP/FTP

RLink

Deciding Which Technology is Right for Your Project

Question 2: Am I (a) calling from Mathematica into another library or environment, or (b) launching and calling Mathematica from another environment?

2a  (Calling out from Mathematica)

Before you write anything, find out if there is a Java or .NET library available that can already do it (there probably is).

If it’s a toss-up between Java and .NET, use Java for portability.

If you need to call C code, or you want to write your own C code, write a MathLink “installable” program, or use LibraryLink. Pick LibraryLink if you ned the highest possible performance (not referring to execution speed of the code itself, but to low overhead per call and fast transfer of large arrays).

If you need extreme performance and have the proper graphics card, consider using CUDALink or OpenCLLink.

Deciding Which Technology is Right for Your Project

Question 2: Am I (a) calling from Mathematica into another library or environment, or (b) launching and calling Mathematica from another environment?

2b  (Launching and calling Mathematica from another language or program)

If you have the option of using Java or a .NET language (C#, VB.NET, F#, etc.), use it. J/Link and .NET/Link are much higher-level toolkits than the C MathLink API, and your life will be much easier.

If you want to use Python, there is a PythonLink bunded with Mathematica (SystemFiles/Links/PythonLink). It allows you to use the MathLink API from Python.

If you want to use C/C++, or another language that can call C libraries, use the MathLink API.